<template>
  <el-container>
    <el-header class="header">
      <i class="el-icon-notebook-2 title"></i>
      <span class="title">博客登陆</span>
    </el-header>
    <el-main class="form">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input v-model="ruleForm.password" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="verificationCode">
          <el-input class="verificationCode" v-model="ruleForm.verificationCode"
                    placeholder="请输入验证码"></el-input>
          <img v-if="codeImg" @click="codeImgHandle" class="codeImg" :src="codeImg" alt="点击更换验证">
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登陆</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button>
            <router-link to="/register">注册</router-link>
          </el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import Element from "element-ui";

export default {
  name: "Login",
  data() {
    return {
      codeImg: '',
      uuid: '',
      ruleForm: {
        username: 'waiman',
        password: '111111',
        verificationCode: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        verificationCode: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ]
      }
    };
  },
  getter: {}
  ,
  methods: {
    submitForm(formName) {
      let _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 登陆
          _this.login();
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    codeImgHandle(e) {
      const _this = this

      this.$axios.get("/api/captchaImage").then(res => {
        console.log(res.data);
        if (res.status == 200 && res.data.code == 0) {
          //  显示base
          _this.codeImg = 'data:image/gif;base64,' + res.data.data.img;
          _this.uuid = res.data.data.uuid;
        }
      }).catch()
      // e.target.src = "/api/kaptcha/getVerificationCode"
    },
    login() {
      // 把 uuid添加到form表单中
      Object.assign(this.ruleForm, {uuid: this.uuid})
      // 登陆方法
      let _this = this
      this.$axios.post('/api/login', this.ruleForm).then(res => {
        if (res.status === 200 && res.data.code == 0) {
          const token = res.headers['authorization']
          _this.$store.commit('SET_TOKEN', token)
          _this.$store.commit('SET_USERINFO', res.data.data)
          _this.$router.push("/blogs")
        } else {
          // 弹窗异常信息
          Element.Message({
            message: res.data.msg,
            type: 'error',
            duration: 2 * 1000
          })
          _this.codeImgHandle();
        }
      })
    }
  },
  created() {
    this.codeImgHandle();
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

.title {
  font-size: 30px;
  font-weight: bolder;
}

.form {
  /*width: 600px;*/
  margin: 100px auto;
}

.header {
  width: 80%;
  margin: 20px auto;
}

.verificationCode {
  float: left;
  width: 55%;
}

.codeImg {
  float: right;
  border-radius: 5px;
  width: 100px;
  height: 38px;
}

</style>
